<?php

namespace app\admin\controller;

use think\Session;
use think\Controller;

use app\admin\model\User as UserModel;

class User extends Controller
{
    private $model;


    public function index()
    {
        $model = new UserModel();

        $users = $model->select();
        $this->assign(['name'=>'web','emps'=>$users, 'list'=>['jack','ming','bing']]);
        return $this->fetch();
    }

    public function del($no)//删除
    {
        if (UserModel::destroy($no)) {
            $this->success('删除成功', url('index'));
        } else {
            $this->error('删除失败');
        }
    }

   
    public function upload(){//上传头像
        //获取上传图片
        $file = request()->file('image');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
               
                $head=$info->getSaveName();//为头像命名
                // $user_id = $_SESSION['n'] ;//接收目前登录账号的用户id
                $user_id=1;
                $model = new UserModel();
                $user = $model->getHead($user_id,$head);//保存头像
                //2、用户数据输出
                $users = $model->getList($user_id);
                $this->assign('users', $users);
                return $this->fetch( 'head',['user_id' => $user_id]);

            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }
     
    //  public function upload()//头像上传测试
    // {
    //     // $user_id = $_SESSION['n'] ;
    //     $user_id=1;
    //     $model = new UserModel();
    //     $users = $model->getList($user_id);
    //     $this->assign('user', $user);
    //     return $this->fetch();
    // }

}